/**
 * embeds in a UOMItemClassEditorDialog GUI entries (text field, comboBox,checkBox, button) for adding fields to ItemClasses
 *  @author zulianp@lu.unisi.ch
 */
package ch.unisi.inf.projects.java.uom.gui.itemclasseditor;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.BoxLayout;
import javax.swing.Box;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;

import ch.unisi.inf.projects.java.uom.gui.UOMImages;
import ch.unisi.inf.projects.java.uom.gui.commands.IconedCommand;

public final class UOMTripleEntryEmbeeder{
	
	private static final long serialVersionUID = -1L;
	private final JTextField textField;
	private final UOMTypeComboBox comboBox;
	private final JCheckBox checkBox;
	private final JPanel deletePanel;
	private UOMItemClassEditorDialog parent;
	private ArrayList<UOMTripleEntryEmbeeder> parentList;
	
		public UOMTripleEntryEmbeeder(final String name,final String type, final boolean isList)
		{
			
			//text field
			this.textField = new JTextField(name);
	        textField.setPreferredSize(new Dimension(150,15));
	        textField.setMinimumSize(textField.getPreferredSize());
	        textField.setCaretPosition(0);
	        textField.addFocusListener(new FocusListener() {
	        	public void focusGained (final FocusEvent ev) {
	        		textField.setSelectionStart(0);
	        		textField.setSelectionEnd(textField.getText().length());
	        	}
	        	public void focusLost (final FocusEvent ev) { return; }
	        });
	        
	        //comboBox 
	        comboBox = new UOMTypeComboBox();
	        if (type != null)
	            comboBox.setSelectedItem(type.toUpperCase());
	       
	        //checkBox
	        checkBox = new JCheckBox("List");
	        checkBox.setSelected(isList);
	        
	        //delete button
	        deletePanel = new JPanel();
	        deletePanel.setLayout(new BoxLayout(deletePanel, BoxLayout.X_AXIS));
	        final JButton deleteButton = new JButton(new IconedCommand("", "Deletes this field", UOMImages.REMOVE.getFileName()) {
	        	private static final long serialVersionUID = -1L;
	        	public void actionPerformed(ActionEvent e){
	        		removeFromParent();
	        	}
	        });
	        deletePanel.add(deleteButton);
	        deletePanel.add(Box.createHorizontalGlue());
	    }
		
		public void embeedIn(final UOMItemClassEditorDialog dialog,final  ArrayList<UOMTripleEntryEmbeeder> list){
			
			removeFromParent();
			parent = dialog;
			final JPanel panel = parent.getFieldPanel();
			panel.add(textField);
			panel.add(comboBox);
			panel.add(checkBox);
			panel.add(deletePanel);
			parentList = list;
			parentList.add(this);
		}
		
		private void removeFromParent(){
			
			if(parent != null ) {
				JPanel panel = parent.getFieldPanel();
				panel.remove(textField);
				panel.remove(comboBox);
				panel.remove(checkBox);
				deletePanel.removeAll();
				panel.remove(deletePanel);
				parent.pack();
				parent = null;
			}
			if(parentList != null){
				parentList.remove(this);
				parentList = null;
			}
		}

		public TripleEntryModel getEntry()
		{
			 return new TripleEntryModel(textField.getText(), (String) comboBox.getSelectedItem(),checkBox.isSelected());
		}
}